package com.woniuxy.shiro.service.impl;

import com.woniuxy.entity.RbacManager;
import com.woniuxy.shiro.dao.LoginDao;
import com.woniuxy.shiro.service.LoginService;
import com.woniuxy.utils.JWTUtil;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * description: LoginServiceImpl <br>
 * date: 2021/12/6 12:24 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    LoginDao loginDao;
    @Override
    public RbacManager login(String username, String password) {
        SimpleHash simpleHash = new SimpleHash("MD5", password, username + "woniuxy");
        String newPassword = simpleHash.toHex();
        RbacManager rbacManager = loginDao.login(username, newPassword);
        //在这个RbacManager对象中封装token
        if(rbacManager!=null){
            String jwt = JWTUtil.sing(username, newPassword);
            rbacManager.setToken(jwt);
            //RbacManager login = loginDao.login(username, password);
            return rbacManager;
        }else {
            return rbacManager;
        }
//        String jwt = JWTUtil.sing(username, newPassword);
//        rbacManager.setToken(jwt);
        //RbacManager login = loginDao.login(username, password);
        //return rbacManager;
    }

    @Override
    public RbacManager loginAuth(String username, String password) {
        RbacManager rbacManager = loginDao.login(username, password);
        return rbacManager;
    }
}
